// pages/inputInfo/inputInfo.js
var serverConstants = require('../../constants/serverConstants.js');
var httpClient = require('../../utils/httpClient.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    nameFocus: true,
    nameInputRed: false,
    location: {
      formatValue: '',
      value: '南京',
      country: '',
      province: '',
      city: '',
      district: '',
      town: '',
      street: ''
    },
    telephone: '',
    registDisabled: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getlocation();
  },
  getlocation: function () {
    var page = this;
    wx.getLocation({
      type: 'wgs84',   //<span class="comment" style="margin:0px;padding:0px;border:none;">默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标</span><span style="margin:0px;padding:0px;border:none;"> </span>
      success: function (res) {
        // success
        var longitude = res.longitude
        var latitude = res.latitude
        page.loadCity(longitude, latitude)
      },
      fail: function (res) {
        wx.openSetting({
          success: function (res) {
            if (res.authSetting.scope.userLocation) {
              page.getlocation();
            }
          }
        }
        );
      }
    });
  },
  loadCity: function (longitude, latitude) {
    var page = this
    wx.request({
      url: 'https://api.map.baidu.com/geocoder/v2/?ak=' + serverConstants.baiduAk + '&location=' + latitude + ',' + longitude + '&output=json',
      data: {},
      header: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        var location = res.data.result.addressComponent;
        location.formatValue = res.data.result.sematic_description + " " + res.data.result.formatted_address;
        if (location.district && location.district != "") {
          location.value = location.district;
        }
        else if (location.city && location.city != "") {
          location.value = location.city;
        }
        page.setData({ location: location });
        page.checkButtonDisabled();
      },
      fail: function () {
        var location = {
          value: "请允许获取位置"
        }
        page.setData({ location: location });
        page.checkButtonDisabled();
      },

    })
  },
  checkButtonDisabled: function () {
    var data = this.data;
    if (!data.location.value || data.location.value == "请允许获取位置" || data.name == "") {
      this.setData({ registDisabled: true });
      return;
    }
    this.setData({ registDisabled: false });
  },
  nameInput: function (e) {
    this.setData({
      nameInputRed: false,
      name: e.detail.value,
    });
    this.checkButtonDisabled();
  },
  telephoneInput: function (e) {
    this.setData({
      telephone: e.detail.value,
    })
    this.checkButtonDisabled();
  },
  regist: function () {
    if (this.data.name == "") {
      this.setData({
        nameFocus: true,
        nameInputRed: true
      });
      return;
    }
    var req = {
      shopName: this.data.name,
      location: this.data.location.formatValue,
    }
    httpClient.post("/shop/create", req, function (result) {
      wx.showModal({
        title: '成功',
        content: '恭喜你，创建商铺成功',
        showCancel: false,
        confirmText: '进入店铺',
        success: function (res) {
          wx.redirectTo({
            url: "/pages/index/index?shopId=" + result
          });
        }
      });
    });
  }
})